package com.fgxia.admin.controller.operation;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.fgxia.admin.service.IAdminOrderService;
import com.ruoyi.business.domain.bo.admin.AdminOrderAddBo;
import com.ruoyi.business.domain.bo.admin.AdminPriceEditBo;
import com.ruoyi.business.domain.bo.admin.AdminUploadBillBo;
import com.ruoyi.business.domain.vo.OrderFeeConfigVo;
import com.ruoyi.business.domain.vo.OrderFeeVo;
import com.ruoyi.business.domain.vo.OrderInfoVo;
import com.ruoyi.business.domain.vo.admin.OrderResultVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.Type1Group;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理订单
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operation/order")
public class AdminOrderController extends BaseController {

    private final IAdminOrderService orderService;

    /**
     * 获取订单费用配置项列表
     */
    // @SaCheckPermission("admin:order:config")
    @GetMapping("/fee/config")
    public R<List<OrderFeeConfigVo>> feeConfig() {
        return R.ok(orderService.selectFeeConfigList());
    }

    /**
     * 获取可改价的收费项目
     */
    // @SaCheckPermission("admin:order:price")
    @GetMapping("/price/list/{orderId}")
    public R<List<OrderFeeVo>> listPrice(@PathVariable Long orderId) {
        return R.ok(orderService.getPriceItemVoList(orderId));
    }


    /**
     * 获取订单详情(编辑用)
     */
    // @SaCheckPermission("order:order:query")
    @GetMapping(value = "/{orderId}")
    public R<OrderInfoVo> getInfo(@PathVariable Long orderId) {
        return R.ok(orderService.selectOrderById(orderId));
    }

    /**
     * 新增订单
     */
    @RepeatSubmit
    @SaCheckPermission("order:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<OrderResultVo> add(@Validated(AddGroup.class) @RequestBody AdminOrderAddBo orderAddBo) {
        return R.ok(orderService.insertOrder(orderAddBo));
    }

    /**
     * 编辑订单
     */
    @RepeatSubmit
    @SaCheckPermission("admin:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<OrderResultVo> edit(@Validated(EditGroup.class) @RequestBody AdminOrderAddBo orderAddBo) {
        return R.ok(orderService.updateOrder(orderAddBo));
    }

    /**
     * 订单改价
     */
    @RepeatSubmit
    @SaCheckPermission("order:order:price")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/price")
    public R<Void> price(@Validated(Type1Group.class) @RequestBody AdminPriceEditBo price) {
        return toAjax(orderService.updateOrderPrice(price));
    }

    /**
     * 上传订单打款凭证
     */
    @RepeatSubmit
    @SaCheckPermission("order:order:payment")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/payment")
    public R<Void> payment(@Validated @RequestBody AdminUploadBillBo payment) {
        return toAjax(orderService.insertPayment(payment));
    }

}
